'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Namespace Configuration
    Public MustInherit Class ConfigTypeKeySection(Of T As System.Configuration.ConfigurationElement)
        Inherits System.Configuration.ConfigurationElementCollection

        Public Sub New()

        End Sub

        Public Overrides ReadOnly Property CollectionType() As System.Configuration.ConfigurationElementCollectionType
            Get
                Return System.Configuration.ConfigurationElementCollectionType.AddRemoveClearMap
            End Get
        End Property

        Protected MustOverride Overloads Overrides Function CreateNewElement() As System.Configuration.ConfigurationElement

        Protected MustOverride Overrides Function GetElementKey(ByVal element As System.Configuration.ConfigurationElement) As Object

        Public Shadows Property AddElementName() As String
            Get
                Return MyBase.AddElementName
            End Get
            Set(ByVal value As String)
                MyBase.AddElementName = value
            End Set
        End Property

        Public Shadows Property ClearElementName() As String
            Get
                Return MyBase.ClearElementName
            End Get
            Set(ByVal value As String)
                MyBase.AddElementName = value
            End Set
        End Property

        Public Shadows ReadOnly Property RemoveElementName() As String
            Get
                Return MyBase.RemoveElementName
            End Get
        End Property

        Public Shadows ReadOnly Property Count() As Integer
            Get
                Return MyBase.Count
            End Get
        End Property

        Public Function Contains(ByVal typeKey As String) As Boolean
            Return BaseGet(typeKey) IsNot Nothing
        End Function

        Public Function Contains(ByVal type As Type) As Boolean
            Return BaseGet(GetTypeKey(type)) IsNot Nothing
        End Function

        Public Function Contains(ByVal element As T) As Boolean
            Return BaseIndexOf(element) > -1
        End Function

        Public Function IndexOf(ByVal element As T) As Integer
            Return BaseIndexOf(element)
        End Function

        Protected Function GetTypeKey(ByVal type As Type) As String
            Return type.ToString.Split("`"c)(0)
        End Function

        Default Public Shadows Property Item(ByVal index As Integer) As T
            Get
                Return CType(BaseGet(index), T)
            End Get
            Set(ByVal value As T)
                If Not (BaseGet(index) Is Nothing) Then
                    BaseRemoveAt(index)
                End If
                BaseAdd(index, value)
            End Set
        End Property

        Default Public Shadows ReadOnly Property Item(ByVal typeKey As String) As T
            Get
                Return DirectCast(BaseGet(typeKey), T)
            End Get
        End Property

        Default Public Shadows ReadOnly Property Item(ByVal type As Type) As T
            Get
                Return DirectCast(BaseGet(GetTypeKey(type)), T)
            End Get
        End Property

        Public Sub Add(ByVal element As T)
            BaseAdd(element)
        End Sub

        Protected Overrides Sub BaseAdd(ByVal element As System.Configuration.ConfigurationElement)
            MyBase.BaseAdd(element, False)
        End Sub

        Public Overloads Sub Remove(ByVal element As T)
            If BaseIndexOf(element) >= 0 Then
                BaseRemove(GetElementKey(element))
            End If
        End Sub

        Public Sub RemoveAt(ByVal index As Integer)
            BaseRemoveAt(index)
        End Sub

        Public Overloads Sub Remove(ByVal typeKey As String)
            BaseRemove(typeKey)
        End Sub

        Public Sub Clear()
            BaseClear()
        End Sub
    End Class
End Namespace